/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package service;

import bean.UserRandom;
import common.Constant;
import java.util.Random;
import org.springframework.stereotype.Service;
import util.Util;

/**
 *
 * @author root
 */
@Service
public class SmsService {

    /**
     * 发送验证码
     * @param phone 
     */
    public void sendSmsCaptcha(String phone) {
        UserRandom userRandom = Constant.phoneRandom.get(phone);
        if (userRandom != null) {
            long userstamp = userRandom.getTimeStamp();
            if ((System.currentTimeMillis() - userstamp) < Constant.INTERVALTIME) {
                return;
            }
        }
        StringBuilder stringBuilder = new StringBuilder();
        Random rdm = new Random(System.currentTimeMillis());
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(rdm.nextInt(10) + "");
        }
        Util.sendSMS(phone, stringBuilder.toString());
        Constant.phoneRandom.put(phone, new UserRandom(stringBuilder.toString(), System.currentTimeMillis()));
    }
}
